package servlet;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
/**
 * Servlet implementation class Operate
 */
@WebServlet("/Operate")
public class Operate extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Operate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
    	response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		String choice=request.getParameter("choice");
		String openid=request.getParameter("openid");
		String result="";
		switch(choice){
			case "insertopenid":{
				Function.insertopenid(openid);
				break;
			}
			case "insertemail":{
				String email=request.getParameter("email");
				Function.insertemail(openid, email);
				break;
			}
			case "insertemailtip":{
				String emailtip=request.getParameter("emailtip");
				try {
					Function.insertemailtip(openid, emailtip);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
			case "insertaccount":{
				String tname=request.getParameter("tname");
				String tip=request.getParameter("tip");
				float money=Float.parseFloat(request.getParameter("money"));
				Function.insertaccount(openid, tname, tip, money);
				break;
			}
			case "insertbudget":{
				float money=Float.parseFloat(request.getParameter("money"));
				System.out.println(money);
				Function.insertbudget(openid, money);
				break;
			}
			case "findaccount":{
				int year=Integer.parseInt(request.getParameter("year"));
				int month=Integer.parseInt(request.getParameter("month"));
				ArrayList<Account> account;
				account=Function.findaccount(openid, year, month);
				result=JSON.toJSON(account).toString();
				break;
			}
			case "findin_out":{
				int year=Integer.parseInt(request.getParameter("year"));
				int month=Integer.parseInt(request.getParameter("month"));
				float[] total=Function.findin_out(openid, year, month);
				result=JSON.toJSON(total).toString();
				break;
			}
			case "findbudget":{
				int year=Integer.parseInt(request.getParameter("year"));
				int month=Integer.parseInt(request.getParameter("month"));
				result=Function.findbudget(openid, year, month)+"";
				break;
			}
			case "findemailmessage":{
				result=Function.findemailmessage(openid);
				break;
			}
			case "findemailtip":{
				result=Function.findemailtip(openid);
				break;
			}
			case "checkemail":{  //如果存在而返回1，不存在则返回0
				String email=request.getParameter("email");
				result=SMTPMXLookup.isAddressValid(email)+"";
				System.out.println("result="+result);
				break;
			}
			case "accountdays":{
				result=Function.accountdays(openid)+"";
				break;
			}
			case "programdata":{
				int year1=Integer.parseInt(request.getParameter("year1"));
				int month1=Integer.parseInt(request.getParameter("month1"));
				int year2=Integer.parseInt(request.getParameter("year2"));
				int month2=Integer.parseInt(request.getParameter("month2"));
				String in_or_out=request.getParameter("in_or_out");
				ArrayList<pdata> list1 ;
				list1=Function.programdata(openid,year1,month1,year2,month2,in_or_out);					
				result=JSONArray.toJSONString(list1);
				break;
			}
			default:break;
		}
		Writer out = response.getWriter(); 
		out.write(result);
		out.flush();
	}   
        

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		doGet(request, response);
	}
	
}
